home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 028a / unzup41e.zip / UNZIP.H < prev    next >
C/C++ Source or Header  |  1991-05-04  |  29KB  |  792 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzip.h
  4.  
  5.   This header file is used by all of the unzip source files.  Its contents
  6.   are divided into six more-or-less separate sections:  OS-dependent includes,
  7.   (mostly) OS-independent defines, typedefs, function prototypes (or "proto-
  8.   types," in the case of non-ANSI compilers), macros, and global-variable
  9.   declarations.
  10.  
  11.   ---------------------------------------------------------------------------*/
  12.  
  13.  
  14.  
  15. /***************************/
  16. /*  OS-Dependent Includes  */
  17. /***************************/
  18.  
  19. #include <stdio.h>       /* this is your standard header for all C compiles */
  20. #include <ctype.h>
  21. #include <errno.h>       /* used in mapped_name() */
  22. #define DECLARE_ERRNO    /* everybody except MSC 6.0 */
  23. #ifdef VMS               /* sigh...you just KNEW someone had to break this.  */
  24. #  include <types.h>     /*  (placed up here instead of in VMS section below */
  25. #  include <stat.h>      /*   because types.h is used in some other headers) */
  26. #else  /* almost everybody */
  27. #  if defined(THINK_C) || defined(MPW) /* for Macs */
  28. #    include <stddef.h>
  29. #  else
  30. #    include <sys/types.h> /* off_t, time_t, dev_t, ... */
  31. #    include <sys/stat.h>  /* Everybody seems to need this. */
  32. #  endif
  33. #endif                   /*   This include file defines
  34.                           *     #define S_IREAD 0x0100  (owner may read)
  35.                           *     #define S_IWRITE 0x0080 (owner may write)
  36.                           *   as used in the creat() standard function.  Must
  37.                           *   be included AFTER sys/types.h for most systems.
  38.                           */
  39.  
  40.  
  41. /*---------------------------------------------------------------------------
  42.     Next, a word from our Unix (mostly) sponsors:
  43.   ---------------------------------------------------------------------------*/
  44.  
  45. #ifdef UNIX               /* On some systems sys/param.h partly duplicates   */
  46. #  include <sys/param.h>  /*  the contents of sys/types.h, so you don't need */
  47.                           /*  (and can't use) sys/types.h. (or param.h???)   */
  48. #  ifndef BSIZE
  49. #    define BSIZE   DEV_BSIZE   /* assume common for all Unix systems */
  50. #  endif
  51.  
  52. #  ifndef BSD
  53. #    define NO_MKDIR            /* for mapped_name() */
  54. #    include <time.h>
  55.      struct tm *gmtime(), *localtime();
  56. #  else   /* BSD */
  57. #    include <sys/time.h>
  58. #    include <sys/timeb.h>
  59. #  endif
  60.  
  61. #else   /* !UNIX */
  62. #  define BSIZE   512           /* disk block size */
  63. #endif
  64.  
  65. #if defined(V7) || defined(BSD)
  66. #  define strchr    index
  67. #  define strrchr   rindex
  68. #endif
  69.  
  70. /*---------------------------------------------------------------------------
  71.     And now, our MS-DOS and OS/2 corner:
  72.   ---------------------------------------------------------------------------*/
  73.  
  74. #ifdef __TURBOC__
  75. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS2    */
  76. #  include <sys/timeb.h>      /* for structure ftime                        */
  77. #  include <mem.h>            /* for memcpy()                               */
  78. #else                         /* NOT Turbo C...                             */
  79. #  ifdef MSDOS                /*   but still MS-DOS, so we'll assume it's   */
  80. #    ifndef MSC               /*   Microsoft's compiler and fake the ID, if */
  81. #      define MSC             /*   necessary (it is in 5.0; apparently not  */
  82. #    endif                    /*   in 5.1 and 6.0)                          */
  83. #    include <dos.h>          /* _dos_setftime()                            */
  84. #  endif
  85. #  ifdef OS2                  /* stuff for DOS and OS/2 family version */
  86. #    ifndef MSC
  87. #      define MSC
  88. #    endif
  89. #    include <os2.h>          /* DosQFileInfo(), DosSetFileInfo()? */
  90. #  endif
  91. #endif
  92.  
  93. #ifdef MSC                    /* defined for all versions of MSC now         */
  94. #  define DOS_OS2             /* Turbo C under DOS, MSC under DOS or OS/2    */
  95. #  ifndef __STDC__            /* MSC 5.0 and 5.1 aren't truly ANSI-standard, */
  96. #    define __STDC__          /*   but they understand prototypes...so       */
  97. #  endif                      /*   they're close enough for our purposes     */
  98. #  if defined(_MSC_VER) && (_MSC_VER >= 600)      /* new with 5.1 or 6.0 ... */
  99. #    undef DECLARE_ERRNO      /* errno is now a function in a dynamic link   */
  100. #  endif                      /*   library (or something)--incompatible with */
  101. #endif                        /*   the usual "extern int errno" declaration  */
  102.  
  103. #ifdef DOS_OS2                /* defined for both Turbo C, MSC */
  104. #  include <io.h>             /* lseek(), open(), setftime(), dup(), creat() */
  105. #endif
  106.  
  107. /*---------------------------------------------------------------------------
  108.     Followed by some VMS (mostly) stuff:
  109.   ---------------------------------------------------------------------------*/
  110.  
  111. #ifdef VMS
  112. #  include <time.h>             /* the usual non-BSD time functions */
  113. #  include <file.h>             /* same things as fcntl.h has */
  114. #  define UNIX                  /* can share most of same code from now on */
  115. #  define RETURN   return_VMS   /* VMS interprets return codes incorrectly */
  116. #else
  117. #  define RETURN   return       /* only used in main() */
  118. #  ifdef V7
  119. #    define O_RDONLY  0
  120. #    define O_WRONLY  1
  121. #    define O_RDWR    2
  122. #  else
  123. #    ifdef MTS
  124. #      include <sys/file.h>     /* MTS uses this instead of fcntl.h */
  125. #    else
  126. #      include <fcntl.h>
  127. #    endif
  128. #  endif
  129. #endif
  130. /*
  131.  *   fcntl.h (above):   This include file defines
  132.  *                        #define O_BINARY 0x8000  (no cr-lf translation)
  133.  *                      as used in the open() standard function.
  134.  */
  135.  
  136. /*---------------------------------------------------------------------------
  137.     And some Mac stuff for good measure:
  138.   ---------------------------------------------------------------------------*/
  139.  
  140. #ifdef THINK_C
  141. #  define MACOS
  142. #  define NOTINT16
  143. #  ifndef __STDC__            /* THINK_C isn't truly ANSI-standard, */
  144. #    define __STDC__          /*   but it understands prototypes...so */
  145. #  endif                      /*   it's close enough for our purposes */
  146. #  include <time.h>
  147. #  include <unix.h>
  148. #  include "macstat.h"
  149. #endif
  150. #ifdef MPW                    /* not tested yet - should be easy enough tho */
  151. #  define MACOS
  152. #  define NOTINT16
  153. #  include <time.h>
  154. #  include <fcntl.h>
  155. #  include "macstat.h"
  156. #endif
  157.  
  158. /*---------------------------------------------------------------------------
  159.     And finally, some random extra stuff:
  160.   ---------------------------------------------------------------------------*/
  161.  
  162. #ifdef __STDC__
  163. #  include <stdlib.h>      /* standard library prototypes, malloc(), etc. */
  164. #  include <string.h>      /* defines strcpy, strcmp, memcpy, etc. */
  165. #else
  166.    char *malloc();
  167.    char *strchr(), *strrchr();
  168.    long lseek();
  169. #endif
  170.  
  171.  
  172.  
  173.  
  174.  
  175. /*************/
  176. /*  Defines  */
  177. /*************/
  178.  
  179. #define INBUFSIZ          BUFSIZ   /* same as stdio uses */
  180. #define DIR_BLKSIZ        64       /* number of directory entries per block
  181.                                     *  (should fit in 4096 bytes, usually) */
  182. #define FILNAMSIZ         (1025)
  183. #ifdef MTS
  184. #  undef FILENAME_MAX              /* the MTS value is too low: use default */
  185. #endif
  186. #ifndef FILENAME_MAX               /* make sure FILENAME_MAX always exists  */
  187. #  define FILENAME_MAX    (FILNAMSIZ - 1)
  188. #endif
  189. #ifdef ZIPINFO
  190. #  define OUTBUFSIZ       BUFSIZ   /* zipinfo needs less than unzip does    */
  191. #else
  192. #  define OUTBUFSIZ       0x2000   /* unImplode needs power of 2, >= 0x2000 */
  193. #endif
  194.  
  195. #define ZSUFX             ".zip"
  196. #define CENTRAL_HDR_SIG   "\120\113\001\002"   /* the infamous "PK" */
  197. #define LOCAL_HDR_SIG     "\120\113\003\004"   /*  signature bytes  */
  198. #define END_CENTRAL_SIG   "\120\113\005\006"
  199.  
  200. #define SKIP              0    /* choice of activities for do_string() */
  201. #define DISPLAY           1
  202. #define FILENAME          2
  203.  
  204. #define DOS_OS2_FAT_      0    /* version_made_by codes (central dir) */
  205. #define AMIGA_            1
  206. #define VMS_              2    /* MAKE SURE THESE ARE NOT DEFINED ON     */
  207. #define UNIX_             3    /* THE RESPECTIVE SYSTEMS!!  (Like, for   */
  208. #define VM_CMS_           4    /* instance, "UNIX":  CFLAGS = -O -DUNIX) */
  209. #define ATARI_            5
  210. #define OS2_HPFS_         6
  211. #define MAC_              7
  212. #define Z_SYSTEM_         8
  213. #define CPM_              9
  214. /* #define TOPS20_   10  (we're going to need this soon...)  */
  215. #define NUM_HOSTS         10   /* index of last system + 1 */
  216.  
  217. #define STORED            0    /* compression methods */
  218. #define SHRUNK            1
  219. #define REDUCED1          2
  220. #define REDUCED2          3
  221. #define REDUCED3          4
  222. #define REDUCED4          5
  223. #define IMPLODED          6
  224. #define NUM_METHODS       7    /* index of last method + 1 */
  225. /* don't forget to update list_files() appropriately if NUM_METHODS changes */
  226.  
  227. #ifndef MACOS
  228. #  define TRUE            1    /* sort of obvious */
  229. #  define FALSE           0
  230. #endif
  231.  
  232. #define UNZIP_VERSION   11     /* compatible with PKUNZIP 1.1 */
  233.  
  234. #ifndef UNZIP_OS               /* not used yet, but will need for Zip  */
  235. #  ifdef UNIX                  /* (could be defined in Makefile or...) */
  236. #    define UNZIP_OS    UNIX_
  237. #  endif
  238. #  ifdef DOS_OS2
  239. #    define UNZIP_OS    DOS_OS2_FAT_
  240. #  endif
  241. #  ifdef VMS
  242. #    define UNZIP_OS    VMS_
  243. #  endif
  244. #  ifdef MTS
  245. #    define UNZIP_OS    UNIX_
  246. #  endif
  247. #  ifdef MACOS
  248. #    define UNZIP_OS    MAC_
  249. #  endif
  250. #  ifndef UNZIP_OS             /* still not defined:  default setting */
  251. #    define UNZIP_OS    UNKNOWN
  252. #  endif
  253. #endif
  254.  
  255. /*---------------------------------------------------------------------------
  256.     Macros for accessing the ULONG header fields.  When NOTINT16 is *not*
  257.     defined, these fields are allocated as arrays of char within the structs.
  258.     This prevents 32-bit compilers from padding the structs so that ULONGs
  259.     start on 4-byte boundaries (this will not work on machines that can ONLY
  260.     access ULONGs if they start on 4-byte boundaries).  If NOTINT16 *is*
  261.     defined, however, the original data are individually copied into working
  262.     structs consisting of UWORDs and ULONGs (which may therefore be padded
  263.     arbitrarily), so the ULONGs are accessed normally.
  264.   ---------------------------------------------------------------------------*/
  265.  
  266. #ifdef NOTINT16
  267. #  define ULONG_(X)   X
  268. #else
  269. #  define ULONG_(X)   (*((ULONG *) (X)))
  270. #endif
  271.  
  272. /*---------------------------------------------------------------------------
  273.     True sizes of the various headers, as defined by Phil Katz--so it is not
  274.     likely that these will ever change.  But if they do, make sure both these
  275.     defines AND the typedefs below get updated accordingly.
  276.   ---------------------------------------------------------------------------*/
  277.  
  278. #define LREC_SIZE     26    /* lengths of local file headers, central */
  279. #define CREC_SIZE     42    /*  directory headers, and the end-of-    */
  280. #define ECREC_SIZE    18    /*  central-dir record, respectively      */
  281.  
  282.  
  283. #define MAX_BITS      13                 /* used in unShrink() */
  284. #define HSIZE         (1 << MAX_BITS)    /* size of global work area */
  285.  
  286. #define LF   10   /* '\n' on ASCII machines.  Must be 10 due to EBCDIC */
  287. #define CR   13   /* '\r' on ASCII machines.  Must be 13 due to EBCDIC */
  288.  
  289. #ifdef EBCDIC
  290. #  define ascii_to_native(c)   ebcdic[(c)]
  291. #endif
  292.  
  293. #ifndef SEEK_SET        /* These should all be declared in stdio.h!  But   */
  294. #  define SEEK_SET  0   /*  since they're not (in many cases), do so here. */
  295. #  define SEEK_CUR  1
  296. #  define SEEK_END  2
  297. #endif
  298.  
  299.  
  300.  
  301.  
  302.  
  303. /**************/
  304. /*  Typedefs  */
  305. /**************/
  306.  
  307. typedef unsigned char    byte;       /* code assumes UNSIGNED bytes */
  308. typedef long             longint;
  309. typedef unsigned short   UWORD;
  310. typedef unsigned long    ULONG;
  311. typedef char             boolean;
  312.  
  313. /*---------------------------------------------------------------------------
  314.     Zipfile layout declarations.  If these headers ever change, make sure the
  315.     ??REC_SIZE defines (above) change with them!
  316.   ---------------------------------------------------------------------------*/
  317.  
  318. #ifdef NOTINT16
  319.  
  320.    typedef byte   local_byte_header[ LREC_SIZE ];
  321. #      define L_VERSION_NEEDED_TO_EXTRACT_0     0
  322. #      define L_VERSION_NEEDED_TO_EXTRACT_1     1
  323. #      define L_GENERAL_PURPOSE_BIT_FLAG        2
  324. #      define L_COMPRESSION_METHOD              4
  325. #      define L_LAST_MOD_FILE_TIME              6
  326. #      define L_LAST_MOD_FILE_DATE              8
  327. #      define L_CRC32                           10
  328. #      define L_COMPRESSED_SIZE                 14
  329. #      define L_UNCOMPRESSED_SIZE               18
  330. #      define L_FILENAME_LENGTH                 22
  331. #      define L_EXTRA_FIELD_LENGTH              24
  332.  
  333.    typedef byte   central_directory_byte_header[ CREC_SIZE ];
  334. #      define C_VERSION_MADE_BY_0               0
  335. #      define C_VERSION_MADE_BY_1               1
  336. #      define C_VERSION_NEEDED_TO_EXTRACT_0     2
  337. #      define C_VERSION_NEEDED_TO_EXTRACT_1     3
  338. #      define C_GENERAL_PURPOSE_BIT_FLAG        4
  339. #      define C_COMPRESSION_METHOD              6
  340. #      define C_LAST_MOD_FILE_TIME              8
  341. #      define C_LAST_MOD_FILE_DATE              10
  342. #      define C_CRC32                           12
  343. #      define C_COMPRESSED_SIZE                 16
  344. #      define C_UNCOMPRESSED_SIZE               20
  345. #      define C_FILENAME_LENGTH                 24
  346. #      define C_EXTRA_FIELD_LENGTH              26
  347. #      define C_FILE_COMMENT_LENGTH             28
  348. #      define C_DISK_NUMBER_START               30
  349. #      define C_INTERNAL_FILE_ATTRIBUTES        32
  350. #      define C_EXTERNAL_FILE_ATTRIBUTES        34
  351. #      define C_RELATIVE_OFFSET_LOCAL_HEADER    38
  352.  
  353.    typedef byte   end_central_byte_record[ ECREC_SIZE+4 ];
  354. /*     define SIGNATURE                         0   space-holder only */
  355. #      define NUMBER_THIS_DISK                  4
  356. #      define NUM_DISK_WITH_START_CENTRAL_DIR   6
  357. #      define NUM_ENTRIES_CENTRL_DIR_THS_DISK   8
  358. #      define TOTAL_ENTRIES_CENTRAL_DIR         10
  359. #      define SIZE_CENTRAL_DIRECTORY            12
  360. #      define OFFSET_START_CENTRAL_DIRECTORY    16
  361. #      define ZIPFILE_COMMENT_LENGTH            20
  362.  
  363.  
  364.    typedef struct local_file_header {                 /* LOCAL */
  365.        byte version_needed_to_extract[2];
  366.        UWORD general_purpose_bit_flag;
  367.        UWORD compression_method;
  368.        UWORD last_mod_file_time;
  369.        UWORD last_mod_file_date;
  370.        ULONG crc32;
  371.        ULONG compressed_size;
  372.        ULONG uncompressed_size;
  373.        UWORD filename_length;
  374.        UWORD extra_field_length;
  375.    } local_file_header;
  376.  
  377.    typedef struct central_directory_file_header {     /* CENTRAL */
  378.        byte version_made_by[2];
  379.        byte version_needed_to_extract[2];
  380.        UWORD general_purpose_bit_flag;
  381.        UWORD compression_method;
  382.        UWORD last_mod_file_time;
  383.        UWORD last_mod_file_date;
  384.        ULONG crc32;
  385.        ULONG compressed_size;
  386.        ULONG uncompressed_size;
  387.        UWORD filename_length;
  388.        UWORD extra_field_length;
  389.        UWORD file_comment_length;
  390.        UWORD disk_number_start;
  391.        UWORD internal_file_attributes;
  392.        ULONG external_file_attributes;
  393.        ULONG relative_offset_local_header;
  394.    } central_directory_file_header;
  395.  
  396.    typedef struct end_central_dir_record {            /* END CENTRAL */
  397.        UWORD number_this_disk;
  398.        UWORD num_disk_with_start_central_dir;
  399.        UWORD num_entries_centrl_dir_ths_disk;
  400.        UWORD total_entries_central_dir;
  401.        ULONG size_central_directory;
  402.        ULONG offset_start_central_directory;
  403.        UWORD zipfile_comment_length;
  404.    } end_central_dir_record;
  405.  
  406.  
  407. #else   /* !NOTINT16:  read data directly into the structure we'll be using */
  408.  
  409.  
  410.    typedef struct local_file_header {                 /* LOCAL */
  411.        byte version_needed_to_extract[2];
  412.        UWORD general_purpose_bit_flag;
  413.        UWORD compression_method;
  414.        UWORD last_mod_file_time;
  415.        UWORD last_mod_file_date;
  416.        byte crc32[4];
  417.        byte compressed_size[4];
  418.        byte uncompressed_size[4];
  419.        UWORD filename_length;
  420.        UWORD extra_field_length;
  421.    } local_file_header;
  422.  
  423.    typedef struct central_directory_file_header {     /* CENTRAL */
  424.        byte version_made_by[2];
  425.        byte version_needed_to_extract[2];
  426.        UWORD general_purpose_bit_flag;
  427.        UWORD compression_method;
  428.        UWORD last_mod_file_time;
  429.        UWORD last_mod_file_date;
  430.        byte crc32[4];
  431.        byte compressed_size[4];
  432.        byte uncompressed_size[4];
  433.        UWORD filename_length;
  434.        UWORD extra_field_length;
  435.        UWORD file_comment_length;
  436.        UWORD disk_number_start;
  437.        UWORD internal_file_attributes;
  438.        byte external_file_attributes[4];
  439.        byte relative_offset_local_header[4];
  440.    } central_directory_file_header;
  441.  
  442.    typedef struct end_central_dir_record {            /* END CENTRAL */
  443.        byte _sig_[4];  /* space-holder only */
  444.        UWORD number_this_disk;
  445.        UWORD num_disk_with_start_central_dir;
  446.        UWORD num_entries_centrl_dir_ths_disk;
  447.        UWORD total_entries_central_dir;
  448.        byte size_central_directory[4];
  449.        byte offset_start_central_directory[4];
  450.        UWORD zipfile_comment_length;
  451.    } end_central_dir_record;
  452.  
  453. #endif  /* !NOTINT16 */
  454.  
  455.  
  456.  
  457.  
  458.  
  459. /*************************/
  460. /*  Function Prototypes  */
  461. /*************************/
  462.  
  463. #ifndef __              /* This amusing little construct was swiped without  */
  464. #  ifdef __STDC__       /*  permission from the fine folks at Cray Research, */
  465. #    define __(X)   X   /*  Inc.  Should probably give them a call and see   */
  466. #  else                 /*  if they mind, but....  Then again, I can't think */
  467. #    define __(X)   ()  /*  of any other way to do this, so maybe it's an    */
  468. #  endif                /*  algorithm?  Whatever, thanks to CRI.  (Note:     */
  469. #endif                  /*  keep interior stuff parenthesized.)              */
  470. /*
  471.  * Toad Hall Note:  Not to worry:  I've seen this somewhere else too,
  472.  * so obviously it's been stolen more than once.
  473.  * That makes it public domain, right?
  474.  */
  475.  
  476. /*---------------------------------------------------------------------------
  477.     Functions in nunzip.c:
  478.   ---------------------------------------------------------------------------*/
  479.  
  480. void   usage                         __( (void) );
  481. int    process_zipfile               __( (void) );
  482. int    find_end_central_dir          __( (void) );
  483. int    process_end_central_dir       __( (void) );
  484. int    list_files                    __( (void) );
  485. int    extract_or_test_files         __( (void) );
  486. int    extract_or_test_member        __( (void) );
  487. int    process_central_file_header   __( (void) );
  488. int    process_local_file_header     __( (void) );
  489.  
  490. /*---------------------------------------------------------------------------
  491.     Functions in file_io.c:
  492.   ---------------------------------------------------------------------------*/
  493.  
  494. int    change_zipfile_attributes __( (int zip_error) );
  495. int    open_input_file           __( (void) );
  496. int    readbuf                   __( (char *buf, register unsigned size) );
  497. int    create_output_file        __( (void) );
  498. int    FillBitBuffer             __( (register int bits) );
  499. int    ReadByte                  __( (UWORD *x) );
  500. int    FlushOutput               __( (void) );
  501. /*
  502.  * static int   WriteBuffer       __( (int fd, unsigned char *buf, int len) );
  503.  * static int   dos2unix          __( (unsigned char *buf, int len) );
  504.  */
  505. void   set_file_time_and_close   __( (void) );
  506.  
  507. /*---------------------------------------------------------------------------
  508.     Macintosh file_io functions:
  509.   ---------------------------------------------------------------------------*/
  510.  
  511. #ifdef MACOS
  512. void   macfstest                 __( (int vrefnum, int wd) );
  513. /*
  514.  * static int   IsHFSDisk        __( (int wAppVRefNum) );
  515.  */
  516. int    mkdir                     __( (char *path, int mode) );
  517. void   SetMacVol                 __( (char *pch, short wVRefNum) );
  518. #endif
  519.  
  520. /*---------------------------------------------------------------------------
  521.     Uncompression functions (all internal compression routines, enclosed in
  522.     comments below, are prototyped in their respective files and are invisi-
  523.     ble to external functions):
  524.   ---------------------------------------------------------------------------*/
  525.  
  526. void   unImplode                __( (void) );                  /* unimplod.c */
  527. /*
  528.  * static void   ReadLengths     __( (sf_tree *tree) );
  529.  * static void   SortLengths     __( (sf_tree *tree) );
  530.  * static void   GenerateTrees   __( (sf_tree *tree, sf_node *nodes) );
  531.  * static void   LoadTree        __( (sf_tree *tree, int treesize, sf_node *nodes) );
  532.  * static void   LoadTrees       __( (void) );
  533.  * static void   ReadTree        __( (register sf_node *nodes, int *dest) );
  534.  */
  535.  
  536. void   unReduce                 __( (void) );                  /* unreduce.c */
  537. /*
  538.  * static void   LoadFollowers   __( (void) );
  539.  */
  540.  
  541. void   unShrink                 __( (void) );                  /* unshrink.c */
  542. /*
  543.  * static void   partial_clear   __( (void) );
  544.  */
  545.  
  546. /*---------------------------------------------------------------------------
  547.     Functions in match.c, mapname.c, and misc.c:
  548.   ---------------------------------------------------------------------------*/
  549.  
  550. int       match         __( (char *string, char *pattern) );      /* match.c */
  551. /*
  552.  * static BOOLEAN   do_list      __( (register char *string, char *pattern) );
  553.  * static void      list_parse   __( (char **patp, char *lowp, char *highp) );
  554.  * static char      nextch       __( (char **patp) );
  555.  */
  556.  
  557. int       mapped_name   __( (void) );                           /* mapname.c */
  558.  
  559. void      UpdateCRC     __( (register unsigned char *s, register int len) );
  560. int       do_string     __( (unsigned int len, int option) );      /* misc.c */
  561. UWORD     makeword      __( (byte *b) );                           /* misc.c */
  562. ULONG     makelong      __( (byte *sig) );                         /* misc.c */
  563. void      return_VMS    __( (int zip_error) );                     /* misc.c */
  564. #ifdef ZMEM
  565.    char   *memset       __( (register char *buf, register char init, register unsigned int len) );
  566.    char   *memcpy       __( (register char *dst, register char *src, register unsigned int len) );
  567. #endif      /* These guys MUST be ifdef'd because their definition  */
  568.             /*  conflicts with the standard one.  Others (makeword, */
  569.             /*  makelong, return_VMS) don't matter.                 */
  570.  
  571.  
  572.  
  573.  
  574.  
  575. /************/
  576. /*  Macros  */
  577. /************/
  578.  
  579. #ifndef min    /* MSC defines this in stdlib.h */
  580. #  define min(a,b)   ((a) < (b) ? (a) : (b))
  581. #endif
  582.  
  583.  
  584. #define LSEEK(abs_offset) {longint request=(abs_offset), inbuf_offset=request%INBUFSIZ, bufstart=request-inbuf_offset;\
  585.    if(bufstart!=cur_zipfile_bufstart) {cur_zipfile_bufstart=lseek(zipfd,bufstart,SEEK_SET);\
  586.    if((incnt=read(zipfd,inbuf,INBUFSIZ))<=0) return(51); inptr=inbuf+inbuf_offset; incnt-=inbuf_offset;\
  587.    }else {incnt+=(inptr-inbuf)-inbuf_offset; inptr=inbuf+inbuf_offset; }}
  588.  
  589. /*
  590.  *  Seek to the block boundary of the block which includes abs_offset,
  591.  *  then read block into input buffer and set pointers appropriately.
  592.  *  If block is already in the buffer, just set the pointers.  This macro
  593.  *  is used by process_end_central_dir (unzip.c) and do_string (misc.c).
  594.  *  A slightly modified version is embedded within extract_or_test_files
  595.  *  (unzip.c).  ReadByte and readbuf (file_io.c) are compatible.
  596.  *
  597.  *  macro LSEEK( abs_offset )
  598.  *    {
  599.  *      longint   request = abs_offset;
  600.  *      longint   inbuf_offset = request % INBUFSIZ;
  601.  *      longint   bufstart = request - inbuf_offset;
  602.  *
  603.  *      if (bufstart != cur_zipfile_bufstart) {
  604.  *          cur_zipfile_bufstart = lseek(zipfd, bufstart, SEEK_SET);
  605.  *          if ((incnt = read(zipfd,inbuf,INBUFSIZ)) <= 0)
  606.  *              return(51);
  607.  *          inptr = inbuf + inbuf_offset;
  608.  *          incnt -= inbuf_offset;
  609.  *      } else {
  610.  *          incnt += (inptr-inbuf) - inbuf_offset;
  611.  *          inptr = inbuf + inbuf_offset;
  612.  *      }
  613.  *    }
  614.  *
  615.  */
  616.  
  617.  
  618. #define OUTB(intc) { *outptr++=intc; if (++outcnt==OUTBUFSIZ) FlushOutput(); }
  619.  
  620. /*
  621.  *  macro OUTB(intc)
  622.  *  {
  623.  *      *outptr++=intc;
  624.  *      if (++outcnt==OUTBUFSIZ)
  625.  *          FlushOutput();
  626.  *  }
  627.  *
  628.  */
  629.  
  630.  
  631. #define READBIT(nbits,zdest) { if (nbits <= bits_left) { zdest = (int)(bitbuf & mask_bits[nbits]); bitbuf >>= nbits; bits_left -= nbits; } else zdest = FillBitBuffer(nbits);}
  632.  
  633. /*
  634.  * macro READBIT(nbits,zdest)
  635.  *  {
  636.  *      if (nbits <= bits_left) {
  637.  *          zdest = (int)(bitbuf & mask_bits[nbits]);
  638.  *          bitbuf >>= nbits;
  639.  *          bits_left -= nbits;
  640.  *      } else
  641.  *          zdest = FillBitBuffer(nbits);
  642.  *  }
  643.  *
  644.  */
  645.  
  646.  
  647. #define NUKE_CRs(buf,len) {register int i,j; for (i=j=0; j<len; (buf)[i++]=(buf)[j++]) if ((buf)[j]=='\r') ++j; len=i;}
  648.  
  649. /*
  650.  *  Remove all the ASCII carriage returns from buffer buf (length len),
  651.  *  shortening as necessary (note that len gets modified in the process,
  652.  *  so it CANNOT be an expression).  This macro is intended to be used
  653.  *  BEFORE A_TO_N(); hence the check for CR instead of '\r'.  NOTE:  The
  654.  *  if-test gets performed one time too many, but it doesn't matter.
  655.  *
  656.  *  macro NUKE_CRs( buf, len )
  657.  *    {
  658.  *      register int   i, j;
  659.  *
  660.  *      for ( i = j = 0  ;  j < len  ;  (buf)[i++] = (buf)[j++] )
  661.  *        if ( (buf)[j] == CR )
  662.  *          ++j;
  663.  *      len = i;
  664.  *    }
  665.  *
  666.  */
  667.  
  668.  
  669. #define TOLOWER(str1,str2) {char *ps1,*ps2; ps1=(str1)-1; ps2=(str2); while(*++ps1) *ps2++=(isupper(*ps1))?tolower(*ps1):*ps1; *ps2='\0';}
  670.  
  671. /*
  672.  *  Copy the zero-terminated string in str1 into str2, converting any
  673.  *  uppercase letters to lowercase as we go.  str2 gets zero-terminated
  674.  *  as well, of course.  str1 and str2 may be the same character array.
  675.  *
  676.  *  macro TOLOWER( str1, str2 )
  677.  *    {
  678.  *      register char   *ps1, *ps2;
  679.  *
  680.  *      ps1 = (str1) - 1;
  681.  *      ps2 = (str2);
  682.  *      while ( *++ps1 )
  683.  *        *ps2++ = (isupper(*ps1)) ?  tolower(*ps1)  :  *ps1;
  684.  *      *ps2='\0';
  685.  *    }
  686.  *
  687.  *  NOTES:  This macro makes no assumptions about the characteristics of
  688.  *    the tolower() function or macro (beyond its existence), nor does it
  689.  *    make assumptions about the structure of the character set (i.e., it
  690.  *    should work on EBCDIC machines, too).  The fact that either or both
  691.  *    of isupper() and tolower() may be macros has been taken into account;
  692.  *    watch out for "side effects" (in the C sense) when modifying this
  693.  *    macro.
  694.  */
  695.  
  696.  
  697. #ifndef ascii_to_native
  698.  
  699. #  define ascii_to_native(c)   (c)
  700. #  define A_TO_N(str1)
  701.  
  702. #else
  703.  
  704. #  define NATIVE   /* Used in main() for '-a' and '-c'. */
  705. #  define A_TO_N(str1) { register unsigned char *ps1; for (ps1 = str1; *ps1; ps1++) *ps1 = (ascii_to_native(*ps1)); }
  706.  
  707. /*
  708.  *   Translate the zero-terminated string in str1 from ASCII to the native
  709.  *   character set. The translation is performed in-place and uses the
  710.  *   ascii_to_native macro to translate each character.
  711.  *
  712.  *   macro A_TO_N( str1 )
  713.  *     {
  714.  *     register unsigned char *ps1;
  715.  *
  716.  *     for ( ps1 = str1; *ps1; ps1++ )
  717.  *       *ps1 = ( ascii_to_native( *ps1 ) );
  718.  *     }
  719.  *
  720.  *   NOTE: Using the ascii_to_native macro means that is it the only part of
  721.  *     unzip which knows which translation table (if any) is actually in use
  722.  *     to produce the native character set. This makes adding new character
  723.  *     set translation tables easy insofar as all that is needed is an
  724.  *     appropriate ascii_to_native macro definition and the translation
  725.  *     table itself. Currently, the only non-ASCII native character set
  726.  *     implemented is EBCDIC but this may not always be so.
  727.  */
  728.  
  729. #endif
  730.  
  731.  
  732.  
  733.  
  734.  
  735. /*************/
  736. /*  Globals  */
  737. /*************/
  738.  
  739.    extern int       tflag;
  740. /* extern int       vflag;    (only used in unzip.c)  */
  741.    extern int       cflag;
  742.    extern int       aflag;
  743.    extern int       dflag;
  744.    extern int       Uflag;
  745.    extern int       V_flag;
  746. #ifdef MACOS
  747.    extern int       hfsflag;
  748. #endif
  749.    extern int       lcflag;
  750.    extern unsigned  f_attr;
  751.    extern longint   csize;
  752.    extern longint   ucsize;
  753.  
  754.    extern short     prefix_of[];
  755. #ifdef MACOS
  756.    extern byte      *suffix_of;
  757.    extern byte      *stack;
  758. #else
  759.    extern byte      suffix_of[];
  760.    extern byte      stack[];
  761. #endif
  762.    extern ULONG     crc32val;
  763.    extern UWORD     mask_bits[];
  764.  
  765.    extern byte      *inbuf;
  766.    extern byte      *inptr;
  767.    extern int       incnt;
  768.    extern UWORD     bitbuf;
  769.    extern int       bits_left;
  770.    extern boolean   zipeof;
  771.    extern int       zipfd;
  772.    extern char      zipfn[];
  773.    extern local_file_header   lrec;
  774.    extern struct stat         statbuf;
  775.    extern longint   cur_zipfile_bufstart;
  776.  
  777.    extern byte      *outbuf;
  778.    extern byte      *outptr;
  779.    extern byte      *outout;
  780.    extern longint   outpos;
  781.    extern int       outcnt;
  782.    extern int       outfd;
  783.    extern char      filename[];
  784.  
  785. #ifdef DECLARE_ERRNO
  786.    extern int       errno;
  787. #endif
  788.  
  789. #ifdef EBCDIC
  790.    extern byte      ebcdic[];
  791. #endif
  792.